@echo off
REM Copy built dynamic libraries to the correct location

echo Setting up library files...

REM Check if source directory exists
if not exist "onnxruntime\build" (
    echo Error: onnxruntime\build directory not found, please run build_onnxruntime.bat first
    exit /b 1
)

REM Create target directories
echo Creating target directories...
if not exist "build\windows\x64\runner\Debug\" mkdir "build\windows\x64\runner\Debug\"
if not exist "build\windows\x64\runner\Release\" mkdir "build\windows\x64\runner\Release\"

REM Try to copy from Release directory (since we only have Release build)
set "SOURCE_DIR_RELEASE=onnxruntime\build\bin\Release"
set "TARGET_DEBUG_DIR=build\windows\x64\runner\Debug"
set "TARGET_RELEASE_DIR=build\windows\x64\runner\Release"

echo Copying library files...

REM Copy Release version
if exist "%SOURCE_DIR_RELEASE%\funasr_flutter.dll" (
    echo Copying Release version: %SOURCE_DIR_RELEASE%\funasr_flutter.dll
    copy "%SOURCE_DIR_RELEASE%\funasr_flutter.dll" "%TARGET_DEBUG_DIR%\"
    copy "%SOURCE_DIR_RELEASE%\funasr_flutter.dll" "%TARGET_RELEASE_DIR%\"
) else (
    echo Release version library file not found: %SOURCE_DIR_RELEASE%\funasr_flutter.dll
    echo Error: funasr_flutter.dll not found, please make sure the library has been built successfully
    exit /b 1
)

REM Copy dependency library files
echo Copying dependency library files...
set "ONNXRUNTIME_LIB_DIR=onnxruntime\onnxruntime-win-x64-1.16.1\lib"
set "FFMPEG_LIB_DIR=onnxruntime\ffmpeg-master-latest-win64-gpl-shared\bin"

if exist "%ONNXRUNTIME_LIB_DIR%" (
    echo Copying onnxruntime dependency libraries...
    xcopy "%ONNXRUNTIME_LIB_DIR%\*.dll" "%TARGET_DEBUG_DIR%\" /Y >nul
    xcopy "%ONNXRUNTIME_LIB_DIR%\*.dll" "%TARGET_RELEASE_DIR%\" /Y >nul
)

if exist "%FFMPEG_LIB_DIR%" (
    echo Copying ffmpeg dependency libraries...
    xcopy "%FFMPEG_LIB_DIR%\*.dll" "%TARGET_DEBUG_DIR%\" /Y >nul
    xcopy "%FFMPEG_LIB_DIR%\*.dll" "%TARGET_RELEASE_DIR%\" /Y >nul
)

echo.
echo Library setup completed!
echo Make sure the following dependency libraries are also properly deployed:
echo 1. onnxruntime.dll
echo 2. ffmpeg related dll files (such as avcodec-60.dll, avformat-60.dll, etc.)
echo.
echo If you still encounter "The specified module could not be found" error at runtime, please check if all dependencies are complete